# @configure_input@

SHELL = /bin/sh
srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
sbindir = @sbindir@
libexecdir = @libexecdir@
datadir = @datadir@
sysconfdir = @sysconfdir@
sharedstatedir = @sharedstatedir@
localstatedir = @localstatedir@
libdir = @libdir@
infodir = @infodir@
mandir = @mandir@
includedir = @includedir@
oldincludedir = /usr/include
arch= @build_cpu@
datarootdir=@datadir@


CC = @CC@
CXX=@CXX@
LD=@CXX@
RANLIB=@RANLIB@
CPPFLAGS = @CPPFLAGS@
CFLAGS = @CFLAGS@
CXXFLAGS = @CXXFLAGS@
OFLAGS=@OFLAGS@
LDFLAGS = @LDFLAGS@
LOADLIBES = @LIBS@
INSTALL = @INSTALL@
AR=ar

pkgdatadir = $(datadir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
pkgincludedir = $(includedir)/@PACKAGE@

top_builddir = .

ACLOCAL = @ACLOCAL@
AUTOCONF = @AUTOCONF@
AUTOMAKE = @AUTOMAKE@
AUTOHEADER = @AUTOHEADER@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@ $(AM_INSTALL_PROGRAM_FLAGS)
INSTALL_DATA = @INSTALL_DATA@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
transform = @program_transform_name@

NORMAL_INSTALL = :
PRE_INSTALL = :
POST_INSTALL = :
NORMAL_UNINSTALL = :
PRE_UNINSTALL = :
POST_UNINSTALL = :
MAKEINFO = @MAKEINFO@
PACKAGE = @PACKAGE@
VERSION = @VERSION@

SUBDIRS =
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs
CONFIG_CLEAN_FILES =
DIST_COMMON =  README ChangeLog Makefile.am \
Makefile.in aclocal.m4 configure configure.ac install-sh missing \
mkinstalldirs


DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)

TAR = gtar
GZIP_ENV = --best

################################################################################
#
# CVD objects
#

CVD_OBJS=   \
			cvd_src/diskbuffer2.o                           \
			cvd_src/deinterlacebuffer.o                     \
			cvd_src/exceptions.o                            \
			cvd_src/image_io.o                              \
			cvd_src/bayer.o                                 \
			cvd_src/morphology.o                            \
			cvd_src/colourspace_convert.o                   \
			cvd_src/draw.o                                  \
			cvd_src/noarch/yuv422.o                         \
			cvd_src/yuv420.o                                \
			cvd_src/image_io/pnm_grok.o                     \
			cvd_src/image_io/bmp.o                          \
			cvd_src/image_io/bmp_read.o                     \
			cvd_src/image_io/bmp_write.o                    \
			cvd_src/image_io/cvdimage.o                     \
			cvd_src/image_io/fits.o                         \
			cvd_src/image_io/fitswrite.o                    \
			cvd_src/image_io/save_postscript.o              \
			cvd_src/image_io/text_write.o                   \
			cvd_src/image_io/text.o                         \
			cvd_src/fast_corner.o                           \
			cvd_src/convolution.o                           \
			cvd_src/nonmax_suppression.o                    \
			cvd_src/timeddiskbuffer.o                       \
			cvd_src/videosource.o                           \
			cvd_src/connected_components.o                  \
			cvd_src/cvd_timer.o                             \
			cvd_src/globlist.o                              \
			@dep_objects@

################################################################################
#
# Shared objects
#

ifeq (@osx_hacks@,yes)
	soname=libcvd.dylib
	soname1=libcvd-@major@.dylib
	soname2=libcvd-@major@.@minor@.dylib

	shared=-dynamiclib
	install_so_target=install-libs-so
else 
ifeq (@cygwin_hacks@,yes)
	dllname=cygcvd.dll
	dllimportname=libcvd.dll.a

	install_so_target=install-libs-dll
else
	soname=libcvd.so
	soname1=libcvd.so.@major@
	soname2=libcvd.so.@major@.@minor@

	set_soname=-Wl,-soname,libcvd.so.@major@

	shared=-shared
	install_so_target=install-libs-so
endif
endif

OBJS=$(CVD_OBJS)

ifeq (@have_shared@,yes)
	install_so=$(install_so_target)
else
	install_so=
	soname:=
endif

all: libcvd.a $(soname) $(dllname) progs 


cyg%.dll lib%.dll.a: lib%.a
	$(LD) -shared -o cyg$*.dll -Wl,--out-implib=lib$*.dll.a -Wl,--export-all-symbols  -Wl,--enable-auto-import -Wl,--whole-archive lib$*.a -Wl,--no-whole-archive $(LDFLAGS) $(LOADLIBES)

$(soname): $(OBJS)
	rm -f $(sos)
	$(LD) $(shared) -o $(soname2) $(OBJS) $(LDFLAGS) $(LOADLIBES) $(set_soname)
	ln -s $(soname2) $(soname1)
	ln -s $(soname1) $(soname)

libcvd.a: $(OBJS)
	$(AR) crvs libcvd.a $(OBJS)
	$(RANLIB) libcvd.a


# Before we compile the test programs we need to augment the
# search paths with the path to libcvd, and also add libcvd to the
# libraries to use
CPPFLAGS = -I. @CPPFLAGS@
ifneq (@osx_hacks@,yes)
    LDFLAGS = -Wl,-R. @LDFLAGS@
endif
LDFLAGS = @LDFLAGS@ -L.
LOADLIBES = @LIBS@

PROGS=@progs@ 
TESTPROGS=@testprogs@

$(PROGS):$(soname)
$(TESTPROGS):$(soname)

testprogs: $(TESTPROGS)
progs: $(PROGS)


@docs@:
	doxygen Doxyfile

test-fast:test/fast_test
	test/fast_test test/fast_test_image/noise.pgm


#If the mandir is older than the lib, then remake. A bit of a hack
man: libcvd.a
	doxygen Doxyfile
	touch man


install: install-progs install-headers install-libs 

install-progs:progs
	mkdir -p $(DESTDIR)$(bindir)
	[ "$(PROGS)" = "" ] || cp $(PROGS) $(DESTDIR)$(bindir)

install-headers:
	mkdir -p $(DESTDIR)$(includedir)
	cp -r cvd $(DESTDIR)$(includedir)






install-libs-dll:$(dllname) $(dllimportname)
	mkdir -p $(DESTDIR)$(bindir)
	cp $(dllname) $(DESTDIR)$(bindir)
	mkdir -p $(DESTDIR)$(libdir)
	cp $(dllimportname) $(DESTDIR)$(libdir)

install-libs-so:$(soname)
	mkdir -p $(DESTDIR)$(libdir)
	rm -f  $(DESTDIR)$(libdir)/$(soname2) $(DESTDIR)$(libdir)/$(soname1) $(DESTDIR)$(libdir)/$(soname)
	cp $(soname2) $(DESTDIR)$(libdir)
	ln -s  $(libdir)/$(soname2) $(DESTDIR)$(libdir)/$(soname1)
	ln -s  $(libdir)/$(soname1) $(DESTDIR)$(libdir)/$(soname)

install-libs-a:libcvd.a
	mkdir -p $(DESTDIR)$(libdir)
	cp libcvd.a $(DESTDIR)$(libdir)

install-libs:$(install_so) install-libs-a
distclean: clean
	/bin/rm -f Makefile config.h config.status config.cache config.log cvd/config.h .deps config.dep_tmp

.deps:
	rm -f .sourcefiles
	find . -name "*.cxx" -o -name "*.cc" -o -name "*.cpp" -o -name "*.c" -o -name "*.C"  | grep -v python > .sourcefiles
	rm -f .deps
	bash generate_dependencies.bash "$(CXX)" "-I. $(CPPFLAGS) $(CXXFLAGS)" > .deps

depend: .deps


.PRECIOUS: %.o
#Let's say a header changed to include a new header.
#That change would cause a rebuild of the .cc file
#but not the .d file. 
#
# Arguments mean:
# -MMD output #include dependencies, but ignore system headers, while
#      the build continues
# -MF output dependencies to a file, so other crud goes to the screen
# -MP make a blank target for dependencies. That way if a dependency is
#     deleted, make won't fail to build stuff 
%.o: %.cc
	$(CXX) $(OFLAGS) -I. $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ -MMD -MP -MF $*.d

%.o: %.cpp
	$(CXX) $(OFLAGS) -I. $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ -MMD -MP -MF $*.d

%.o: %.cxx
	$(CXX) $(OFLAGS) -I. $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ -MMD -MP -MF $*.d

%.o: %.C
	$(CXX) $(OFLAGS) -I. $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ -MMD -MP -MF $*.d

%.o: %.s
	as -g -o $@ $<


%: %.o
	$(CXX) -o $@ $< $(LDFLAGS) -lcvd  $(LOADLIBES)

printlibs:
	@echo $(LOADLIBES)




.PHONY: test

REGRESSIONS=distance_transform_test fast_corner_test load_and_save image_ref convolution
REGRESSION_OUT=$(patsubst %,tests/%.out, $(REGRESSIONS))

test:$(REGRESSION_OUT)
.PRECIOUS:tests/%.test
tests/%.test: tests/%.cc 
	$(CXX) -I. $(CPPFLAGS) $(CXXFLAGS) $< -o $@ -O0 -L. -lcvd -MMD -MP -MF tests/$*.d

.PRECIOUS:tests/%.out
tests/%.out: tests/%.test
	LD_LIBRARY_PATH=. ./$<  && touch $@



.PHONY: examples
EXAMPLES=colourmaps distance_transform tensor_voting

EXAMPLE_PROGS=$(patsubst %,examples/%, $(EXAMPLES))

examples:$(EXAMPLE_PROGS)

$(EXAMPLE_PROGS): % : %.cc
	$(CXX) -I. $(CPPFLAGS) $(CXXFLAGS) $< -o $@ -O3 -L. -lcvd


clean:
	rm -f tmp libcvd.a $(sos) $(dllname) $(ddllname) $(dllimportname) $(ddllimportname)
	find . -name '*.o' -o -name '*.d' | xargs rm -f
	find progs/ -perm /u+x -type f | xargs rm -f
	rm -rf html man
	rm -f tests/*.out
	rm -f tests/*.test
	rm -rf $(EXAMPLE_PROGS)




include $(wildcard cvd_src/*.d)
include $(wildcard cvd_src/*/*.d)
include $(wildcard tests/*.d)
